<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2022/3/26
 * Time: 16:42
 */

namespace app\api\controller;
use app\common\controller\Api;
use think\Db;
use think\Request;

class RmProgramme extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\sign_up\RmProgramme;

    }
    /**
     * 热门活动列表
     */
    public function list()
    {
        if (!Request::instance()->isPost())
            $this->error('请求方式不正确');
        $page = input('post.page',1);
        $limit = input('post.limit',10);

        $where = [];
        $order = [];
        if($course_type_id = input('post.course_type_id'))
        {
            $where['ed_programme.course_type_id'] = $course_type_id;
        }
        if($course_type_purpose_id = input('post.course_type_purpose_id'))
        {
            $where['ed_programme.course_type_purpose_id'] = ['like','%'.$course_type_purpose_id.'%'];
        }
        if($people_number = input('post.people_number'))
        {
            $where['ed_programme.people_number'] = $people_number;
        }
        if($days = input('post.days'))
        {
            $where['ed_programme.days'] = $days;
        }

        if($heat = input('post.heat'))
        {
            $order['heat'] = $heat;
        }

        if($satisfaction = input('post.satisfaction'))
        {
            $order['satisfaction'] = $satisfaction;
        }

        if($price = input('post.price'))
        {
            $order['price'] = $price;
        }

        $list = $this->model
            ->with(['type'])
            ->where($where)
            ->order($order)
            ->paginate($limit,false,['page'=>$page]);

        foreach ($list as $row) {
            $purpose = Db::name('course_type_purpose')->where(['id'=>['in',$row['course_type_purpose_id']]])->field('id,title')->select();
            $row['purpose'] = $purpose;
            $row->visible(['id','title','image','product_detail','satisfaction','people_number','days','paid_view','price','purpose','paid_download','createtime']);
            $row->visible(['type']);
            $row->getRelation('type')->visible(['title']);

        }
        if($list)
            $this->success('',$list);
        $this->error('暂无数据');

    }

    /**
     * 项目详情
     */
    public function detail()
    {
        if (!Request::instance()->isGet())
            $this->error('请求方式不正确');
        if(!$id=input('get.id'))
            $this->error('缺少参数');

        $info = $this->model->get($id);
        if(!$info)
            $this->error('缺少参数');
        if($info['images']!='')
        {
            $info['images'] = explode(',',$info['images']);
        }
        $this->success('',$info);
    }
}